/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.collabframework.texteditor.server;

import java.awt.event.KeyEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author robert
 */
public class TextServer extends UnicastRemoteObject implements ITextServer {

    private String localCopy;
    private int line,  column;
    private IDocServer doc;

    public TextServer() throws RemoteException {
        super();
        try {
            doc = (IDocServer) Naming.lookup("//localhost/DocServer");
        } catch (NotBoundException ex) {
            Logger.getLogger(TextServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(TextServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(TextServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void handleData(KeyEvent evnt) throws RemoteException {
        if (evnt.getKeyCode() == KeyEvent.VK_LEFT) {
            column = (column == 0) ? 0 : --column;
            System.out.println("Linke Taste: " + line + " - " + column);
        } else if (evnt.getKeyCode() == KeyEvent.VK_RIGHT) {
            //TODO hier ist noch ein Problem, wenn man in der letzten Zeile versucht nach rechts zu gehen. Da kommt es zu einer Nullpointer-Exception
            int max = doc.size(line);
            if (column < max) {
                column++;
            } else {
                column = 0;
                max = doc.lineCount();
                line = (line < max) ? ++line : max;
            }
            System.out.println("Rechte Taste: " + line + " - " + column);
        } else if (evnt.getKeyCode() == KeyEvent.VK_UP) {
            line = (line > 0) ? --line : 0;
            System.out.println("Taste hoch: " + line + " - " + column);
        } else if (evnt.getKeyCode() == KeyEvent.VK_DOWN) {
            int max = doc.lineCount();
            line = (line < max) ? ++line : max;
            System.out.println("Taste unten: " + line + " - " + column);
        } else if (evnt.getKeyCode() == KeyEvent.VK_DELETE) {
            int max = doc.size(line);
            if (column < max) {
                System.out.println(doc.getText());
                doc.remove(line, column);
                System.out.println(doc.getText());
                System.out.println("Delete");
            }
        } else if (evnt.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
            column = (column == 0) ? 0 : --column;
            doc.remove(line, column);
        } else if (!evnt.isActionKey()) {
            //TODO die Shift-Taste wird noch nicht richtig mitbehandelt
            doc.add(line, column, evnt.getKeyChar());
            column++;
        }

    }

    public String getText() throws RemoteException {
        return doc.getText();
    }

    public int getLine() throws RemoteException{
        return line;
    }

    public int getColumn() throws RemoteException{
        return column;
    }
}
